package com.nsgf.library.ui.page.home;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.kunminx.binding_recyclerview.adapter.BaseDataBindingAdapter;
import com.kunminx.binding_recyclerview.adapter.SimpleDataBindingAdapter;
import com.zhht.htzx.BR;
import com.zhht.htzx.R;
import com.nsgf.library.data.bean.BusinessBean;
import com.nsgf.library.data.response.ResponseStatus;
import com.zhht.htzx.databinding.FragmentNsgfManagerBinding;
import com.zhht.htzx.databinding.ItemHomeBusinessFragmentBinding;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.page.BaseFragment;
import com.nsgf.library.ui.page.StateHolder;
import com.nsgf.library.ui.product.BusinessDetailActivity;
import com.nsgf.library.utils.SysUtil;
import com.nsgf.library.utils.UnitTo;

import java.util.ArrayList;
import java.util.List;

public class ManagerFragment extends BaseFragment {

    private ManagerStateHolder mStates;


    public static ManagerFragment newInstance(int type) {
        ManagerFragment fragment = new ManagerFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;

    }

    private int getType() {
        return getArguments().getInt("type");
    }

    private FragmentNsgfManagerBinding mBinding;

    private AccountRequester mAccountRequester;

    @Override
    protected void initViewModel() {
        mStates = getFragmentScopeViewModel(ManagerStateHolder.class);
        mAccountRequester = new AccountRequester();
    }

    private SimpleDataBindingAdapter<BusinessBean, ItemHomeBusinessFragmentBinding> mAdapter;

    @Override
    protected DataBindingConfig getDataBindingConfig() {

        mAdapter = new SimpleDataBindingAdapter<BusinessBean, ItemHomeBusinessFragmentBinding>(getActivity(),
                R.layout.item_home_business_fragment, new DiffUtil.ItemCallback<BusinessBean>() {
            @Override
            public boolean areItemsTheSame(@NonNull BusinessBean oldItem, @NonNull BusinessBean newItem) {
                return false;
            }

            @Override
            public boolean areContentsTheSame(@NonNull BusinessBean oldItem, @NonNull BusinessBean newItem) {
                return false;
            }
        }) {
            @Override
            protected void onBindItem(ItemHomeBusinessFragmentBinding binding, BusinessBean item, RecyclerView.ViewHolder holder) {
                binding.setVm(item);
                binding.setClick(new ClickItemProxy());

            }
        };

        return new DataBindingConfig(R.layout.fragment_nsgf_manager, BR.vm, mStates)
                .addBindingParam(BR.adapter, mAdapter)
                .addBindingParam(BR.click, new ClickProxy());
    }

    public class ClickItemProxy {

        public void copyAddress(BusinessBean item) {
            UnitTo.copyStr(getActivity(), item.getAddress());
        }

        public void callPhone(BusinessBean item) {
            SysUtil.callPhone(getActivity(), item.getContactMobile());
        }

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getLifecycle().addObserver(mAccountRequester);
        mBinding = (FragmentNsgfManagerBinding) getBinding();

        String title = "";
        mBinding.titleBarBack.setVisibility(GONE);
        if (getType() == 0) {
            title = "直营店";
            mBinding.titleBarBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    getActivity().finish();
                }
            });
            mBinding.titleBarBack.setVisibility(VISIBLE);
        } else if (getType() == 1) {
            title = "联盟店";
        } else if (getType() == 2) {
            title = "社群";
        } else if (getType() == 3) {
            title = "直播";
        } else title = "";
        mStates.titleBar.set(title);

        mBinding.recyclerView.setItemAnimator(null);
        if (getType() == 2 || getType() == 3) {
            mBinding.ivListNotData.setText("正在建设中,敬请期待...");
            mBinding.ivListNotData.setVisibility(VISIBLE);
        } else {
            mAccountRequester.queryDirectStoreOrQueryFranchiseStore(getType());
            mBinding.ivListNotData.setText("暂无相关数据...");
        }
        mAccountRequester.getBusinessResult().observe(this, new Observer<ResponseStatus<List<BusinessBean>>>() {
            @Override
            public void onChanged(ResponseStatus<List<BusinessBean>> listResponseStatus) {
                if (listResponseStatus.getResponseCode() == 0) {
                    for (BusinessBean bean : listResponseStatus.getData()) {
                        bean.setTypeName(getType() == 0 ? "直营店铺" : "联盟店铺");
                    }
                    if (!listResponseStatus.getData().isEmpty())
                        mBinding.ivListNotData.setVisibility(GONE);
                    else
                        mBinding.ivListNotData.setVisibility(VISIBLE);
                    mAdapter.submitList(listResponseStatus.getData());
                    mAdapter.notifyDataSetChanged();
                }
            }
        });

        mAdapter.setOnItemClickListener(new BaseDataBindingAdapter.OnItemClickListener<BusinessBean>() {
            @Override
            public void onItemClick(int viewId, BusinessBean item, int position) {
                startActivity(new Intent()
                        .putExtra("title", "店铺详情")
                        .putExtra("type", getType())
                        .putExtra("id", item.getId())
                        .setClass(getActivity(), BusinessDetailActivity.class));
            }
        });
        mAdapter.notifyDataSetChanged();

    }

    public static class ManagerStateHolder extends StateHolder {

        public final State<List<BusinessBean>> list = new State<>(new ArrayList<>());

    }


    public class ClickProxy {

    }
}
